package com.itheima.service.serviceimpl;

import com.itheima.common.utils.POIUtils;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingResult;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 根据日期修改预约设置
     *
     * @param orderSetting
     */
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //将orderDate转成LocalDate类型
        LocalDate orderDateLocalDate = orderDate.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();

        log.info("修改预约设置：{}", orderDateLocalDate);

        if (orderSettingDao.getOrderSettingByOrderDate(orderDateLocalDate) == null) {
            //如果该日期没有预约设置，则新增预约设置
            orderSettingDao.addOrderSetting(orderSetting);
            return;
        }
        //如果该日期有预约设置，则修改预约设置
        orderSettingDao.editNumberByOrderDate(orderDateLocalDate, orderSetting.getNumber());
    }



    /**
     * 上传预约设置
     *
     * @param fileName
     * @throws Exception
     */
    @Override
    public void upload(MultipartFile fileName) throws Exception {
        List<String[]> list = POIUtils.readExcel(fileName);
        list.forEach(item -> {
            String orderDate = item[0].replace("/", "-");
            LocalDate orderDateLocalDate = LocalDate.parse(orderDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            log.info("获取到的数据：{}", orderDateLocalDate);
            log.info("获取到的数据：{}", item[1]);

            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(Date.from(orderDateLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            log.info("获取到的数据：{}", Date.from(orderDateLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            orderSetting.setNumber(Integer.parseInt(item[1]));

            if (orderSettingDao.getOrderSettingByOrderDate(orderDateLocalDate) == null) {
                //如果该日期没有预约设置，则新增预约设置
                orderSettingDao.addOrderSetting(orderSetting);
                return;
            }

            //如果该日期有预约设置，则修改预约设置
            orderSettingDao.editNumberByOrderDate(orderDateLocalDate, orderSetting.getNumber());

        });


    }

    /**
     * 根据月份获取预约设置
     *
     * @param month
     * @return
     */
    @Override
    public List<OrderSettingResult> getOrderSettingByMonth(String month) {
        LocalDate monthDate = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate firstDay = monthDate.with(TemporalAdjusters.firstDayOfMonth());
        log.info("获取月份的第一天：{}", firstDay);
        LocalDate lastDay = monthDate.with(TemporalAdjusters.lastDayOfMonth());
        log.info("获取月份的最后一天：{}", lastDay);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(firstDay, lastDay);
        List<OrderSettingResult> orderSettingResults = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            OrderSettingResult orderSettingResult = new OrderSettingResult(orderSetting.getOrderDate().getDate(), orderSetting.getNumber(), orderSetting.getReservations());
            orderSettingResults.add(orderSettingResult);
        }
        return orderSettingResults;
    }

    @Override
    public void download(String fileName) {

    }
}
